home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / sound / players / maplay_t.z / maplay_t / header.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  6.6 KB  |  243 lines

  1. /*
  2.  *  @(#) header.c 1.6, last edit: 2/21/94 18:26:00
  3.  *  @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
  4.  *  @(#) Berlin University of Technology
  5.  *
  6.  *  This program is free software; you can redistribute it and/or modify
  7.  *  it under the terms of the GNU General Public License as published by
  8.  *  the Free Software Foundation; either version 2 of the License, or
  9.  *  (at your option) any later version.
  10.  *
  11.  *  This program is distributed in the hope that it will be useful,
  12.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  *  GNU General Public License for more details.
  15.  *
  16.  *  You should have received a copy of the GNU General Public License
  17.  *  along with this program; if not, write to the Free Software
  18.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21. #include <sys/types.h>
  22. #include <unistd.h>
  23. #include <iostream.h>
  24. #include <iomanip.h>
  25. #include <stdlib.h>
  26. #include "all.h"
  27. #include "header.h"
  28. #include "ibitstream.h"
  29.  
  30.  
  31. const uint32 Header::frequencies[3] = { 44100, 48000, 32000 };
  32.  
  33.  
  34. bool Header::read_header (Ibitstream *stream, Crc16 **crcp)
  35. {
  36.   uint32 headerstring;
  37.  
  38.   if (!stream->get_header (&headerstring))
  39.     return False;
  40.  
  41.   if ((headerstring & 0xFFF00000) != 0xFFF00000)
  42.   {
  43.     cerr << "invalid syncword 0x" << hex << setw (3) << setfill ('0')
  44.      << ((headerstring & 0xFFF00000) >> 20)
  45.      << " found at fileoffset " << dec
  46.      << lseek (stream->filedescriptor (), 0, SEEK_CUR) - 4 << '\n';
  47.     return False;
  48.   }
  49.  
  50.   if ((h_layer = (headerstring >> 17) & 3) == 0)
  51.   {
  52.     cerr << "unknown layer identifier found!\n";
  53.     exit (1);
  54.   }
  55.   h_layer = 4 - h_layer;        // now 1 means Layer I and 3 means Layer III
  56.   if (h_layer == 3)
  57.   {
  58.     cerr << "Sorry, Layer III not implemented!\n";
  59.     exit (1);
  60.   }
  61.   h_protection_bit = (headerstring >> 16) & 1;
  62.   if ((h_bitrate_index = (headerstring >> 12) & 0xF) == 15)
  63.   {
  64.     cerr << "unknown bitrate index found!\n";
  65.     exit (1);
  66.   }
  67.   if (!h_bitrate_index)
  68.   {
  69.     cerr << "free format not yet implemented!\n";
  70.     exit (1);
  71.   }
  72.  
  73.   if ((h_sample_frequency = (e_sample_frequency)((headerstring >> 10) & 3)) == 3)
  74.   {
  75.     cerr << "unknown sample frequency!\n";
  76.     exit (1);
  77.   }
  78.   h_padding_bit = (headerstring >> 9) & 1;
  79.   h_mode = (e_mode)((headerstring >> 6) & 3);
  80.   if (h_layer == 2)
  81.     // testing validity of mode and bitrate:
  82.     if ((((h_bitrate_index >= 1 && h_bitrate_index <= 3) || h_bitrate_index == 5) &&
  83.      h_mode != single_channel) ||
  84.     (h_bitrate_index >= 11 && h_mode == single_channel))
  85.     {
  86.       cerr << "illegal combination of mode and bitrate in a layer II stream:\n"
  87.           "  mode: " << mode_string ()
  88.        << "\n  bitrate: " << bitrate_string () << '\n';
  89.       exit (1);
  90.     }
  91.   h_mode_extension = (headerstring >> 4) & 3;
  92.   if (h_mode == joint_stereo)
  93.     h_intensity_stereo_bound = (h_mode_extension << 2) + 4;
  94.   else
  95.     h_intensity_stereo_bound = 0;        // should never be used
  96.   h_copyright = (headerstring >> 3) & 1;
  97.   h_original = (headerstring >> 2) & 1;
  98.  
  99.   // calculate number of subbands:
  100.   if (h_layer == 1)
  101.     h_number_of_subbands = 32;
  102.   else
  103.   {
  104.     uint32 channel_bitrate = h_bitrate_index;
  105.  
  106.     // calculate bitrate per channel:
  107.     if (h_mode != single_channel)
  108.       if (channel_bitrate == 4)
  109.     channel_bitrate = 1;
  110.       else
  111.     channel_bitrate -= 4;
  112.  
  113.     if (channel_bitrate == 1 || channel_bitrate == 2)
  114.       if (h_sample_frequency == thirtytwo)
  115.     h_number_of_subbands = 12;
  116.       else
  117.     h_number_of_subbands = 8;
  118.     else
  119.       if (h_sample_frequency == fourtyeight || (channel_bitrate >= 3 && channel_bitrate <= 5))
  120.     h_number_of_subbands = 27;
  121.       else
  122.     h_number_of_subbands = 30;
  123.   }
  124.  
  125.   if (h_intensity_stereo_bound > h_number_of_subbands)
  126.     h_intensity_stereo_bound = h_number_of_subbands;
  127.  
  128.   // read framedata:
  129.   if (!stream->read_frame (calculate_framesize ()))
  130.     return False;
  131.  
  132.   if (!h_protection_bit)
  133.   {
  134.     // frame contains a crc checksum
  135.     checksum = (uint16)stream->get_bits (16);
  136.     if (!crc)
  137.       crc = new Crc16;
  138.     crc->add_bits (headerstring, 16);
  139.     *crcp = crc;
  140.   }
  141.   else
  142.     *crcp = (Crc16 *)0;
  143.  
  144.   return True;
  145. }
  146.  
  147.  
  148. uint32 Header::calculate_framesize ()
  149. /* calculates framesize in bytes excluding header size */
  150. {
  151.   static const int32 bitrates_layer_1[15] = {
  152.     0 /*free format*/, 32000, 64000, 96000, 128000, 160000, 192000,
  153.     224000, 256000, 288000, 320000, 352000, 384000, 416000, 448000 };
  154.   static const int32 bitrates_layer_2[15] = {
  155.     0 /*free format*/, 32000, 48000, 56000, 64000, 80000, 96000,
  156.     112000, 128000, 160000, 192000, 224000, 256000, 320000, 384000 };
  157.   static const samplefrequencies[3] = { 44100, 48000, 32000 };
  158.   uint32 framesize;
  159.  
  160.   if (h_layer == 1)
  161.   {
  162.     framesize = (12 * bitrates_layer_1[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  163.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  164.       ++framesize;
  165.     framesize <<= 2;        // one slot is 4 bytes long
  166.   }
  167.   else
  168.   {
  169.     framesize = (144 * bitrates_layer_2[h_bitrate_index]) / samplefrequencies[h_sample_frequency];
  170.     if (h_sample_frequency == fourtyfour_point_one && h_padding_bit)
  171.       ++framesize;
  172.   }
  173.  
  174.   return framesize - 4;        // subtract header size
  175. }
  176.  
  177.  
  178. const char *Header::layer_string (void)
  179. {
  180.   switch (h_layer)
  181.   {
  182.     case 1:
  183.       return "I";
  184.     case 2:
  185.       return "II";
  186.     case 3:
  187.       return "III (not implemented!)";
  188.   }
  189.   return NULL;            // dummy
  190. }
  191.  
  192.  
  193. const char *Header::bitrate_string (void)
  194. {
  195.   static const char *layer1_bitrates[16] = {
  196.     "free format", "32 kbis/s", "64 kbit/s", "96 kbit/s", "128 kbit/s", "160 kbit/s", 
  197.     "192 kbit/s", "224 kbit/s", "256 kbit/s", "288 kbit/s", "320 kbit/s", "352 kbit/s",
  198.     "384 kbit/s", "416 kbit/s", "448 kbit/s", "forbidden"
  199.   };
  200.   static const char *layer2_bitrates[16] = {
  201.     "free format", "32 kbis/s", "48 kbit/s", "56 kbit/s", "64 kbit/s", "80 kbit/s", 
  202.     "96 kbit/s", "112 kbit/s", "128 kbit/s", "160 kbit/s", "192 kbit/s", "224 kbit/s",
  203.     "256 kbit/s", "320 kbit/s", "384 kbit/s", "forbidden"
  204.   };
  205.  
  206.   if (h_layer == 1)
  207.     return layer1_bitrates[h_bitrate_index];
  208.   else
  209.     return layer2_bitrates[h_bitrate_index];
  210. }
  211.  
  212.  
  213. const char *Header::sample_frequency_string (void)
  214. {
  215.   switch (h_sample_frequency)
  216.   {
  217.     case thirtytwo:
  218.       return "32 kHz";
  219.     case fourtyfour_point_one:
  220.       return "44.1 kHz";
  221.     case fourtyeight:
  222.       return "48 kHz";
  223.   }
  224.   return NULL;            // dummy
  225. }
  226.  
  227.  
  228. const char *Header::mode_string (void)
  229. {
  230.   switch (h_mode)
  231.   {
  232.     case stereo:
  233.       return "stereo";
  234.     case joint_stereo:
  235.       return "joint stereo";
  236.     case dual_channel:
  237.       return "dual channel";
  238.     case single_channel:
  239.       return "single channel";
  240.   }
  241.   return NULL;            // dummy
  242. }
  243.